using System;
using System.Runtime.InteropServices;
using ScenarioFramework.Modules.PCM.DataModel;
using ScenarioFramework.Modules.PCM.Database;

using ScenarioFramework.WorkflowEngine.DataModel.WorkflowDataTypes;


namespace ScenarioFramework.WAPI
{
	/// <summary>
	/// Descrizione di riepilogo per wrpWAPIRequest.
	/// </summary>
	public class DataTypeException : System.Exception 
	{
	}

	/// <summary>
	/// Descrizione di riepilogo per wrpWAPIRequest.
	/// </summary>
	public class DataTypes
	{
		/// <summary>
		/// Initializes a new instance  of the <see cref="DataTypes"/> class.
		/// </summary>
		public DataTypes()
		{
		}

		/// <summary>
		/// Gets the data types.
		/// </summary>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		//[ DllImport("WAPI.Dll", EntryPoint="getDataTypes")]
		private static String GetDataTypes(ref Response response)
		{
			Response wapiResponse = new Response();
			string sDB = Session.GetScenarioDBDirectory(ref wapiResponse);
			sDB+="\\Common\\DataTypes.xml";

			return bOSdotNET.IO.FileSystem.GetFileContent(sDB, false);
		}

		/// <summary>
		/// Gets the data types.
		/// </summary>
		/// <param name="sDataType">Type of the s data.</param>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		[ DllImport("WAPI.Dll", EntryPoint="getDataType")]
		private static extern String GetDataTypes(string sDataType, ref Response response);

		/// <summary>
		/// Gets the values.
		/// </summary>
		/// <param name="sDataType2Search">The s data type2 search.</param>
		/// <returns></returns>
		private static XMLEnumerationValues GetValues(string sDataType2Search)
		{
			Response wapiResponse = new Response ();
			string sDataType = DataTypes.GetDataTypes (sDataType2Search, ref wapiResponse);
			if ( wapiResponse.iCode == 0 )
			{
				XMLDataType datatype = new XMLDataType ();
				datatype.Parse (sDataType);

				if ( datatype.XMLEnumerationValues.Count == 0 ) 
				{
					/*XMLEnumerationValues vValues = new XMLEnumerationValues();
					try
					{
						BasicBusinessObjectCollection vRecords = null;
						vRecords = PCMDataBase.Instance().GetBusinessObjectRecords (sDataType2Search, null);
						if (vRecords != null) 
						{
							for ( int i=0; i<vRecords.Count; i++)
							{
								BasicBusinessObject dummyObj = vRecords[i];
								XMLDataTypeValue vValue = new XMLDataTypeValue();
								vValue.Key = dummyObj.Id.ToString();
								vValue.Value = dummyObj.Item;

								vValues.Add ( vValue);
							}
						}
					}
					catch ( DBAdapterException )
					{
					}
					catch
					{
					}

					return vValues;
                     * */
				}

				return datatype.XMLEnumerationValues;
			}
			else
			{
				throw new DataTypeException();
			}
		}

		private static string GetValue(string sDataType, string sKey)
		{
			XMLEnumerationValues vValues = DataTypes.GetValues(sDataType);
			XMLDataTypeValue xmlValue = vValues.GetValue (sKey);

			if ( xmlValue == null )
			{
				throw new DataTypeException();
			}
			else
			{
				return xmlValue.Value;
			}

			/*
			 * Response wapiResponse = new Response ();
			string sXML = WAPI.DataTypes.GetDataTypes (sDataType, ref wapiResponse);
			if ( wapiResponse.iCode == 0 )
			{
				XMLDataType datatype = new XMLDataType ();
				datatype.Parse (sXML);

				XMLDataTypeValue xmlValue = datatype.XMLEnumerationValues.GetValue (sKey);

				if ( xmlValue == null )
				{
					throw new DataTypeException();
				}
				else
				{
					return xmlValue.Value;
				}
			}
			else
			{
				throw new DataTypeException();
			}
			*/
		}


	}
}
